package org.example.springbook.controller;

import lombok.Value;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.example.springbook.model.BookInfo;
import org.example.springbook.model.PageRequest;
import org.example.springbook.model.PageResult;
import org.example.springbook.model.Result;
import org.example.springbook.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;

    @RequestMapping(value = "/addBook",produces="application/json")
    public Result addBook(BookInfo bookInfo) {
        //进行参数检验
        log.info("添加图书：bookInfo: {}"+ bookInfo);
        if(!StringUtils.hasLength(bookInfo.getBookName())
        || !StringUtils.hasLength(bookInfo.getAuthor())
        || !StringUtils.hasLength(bookInfo.getPublish())
        || bookInfo.getCount()<=0
        || bookInfo.getPrice()==null) {
            return Result.fail(false,"参数不合法");
        }
        try{
            bookService.insertBook(bookInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(false,e.getMessage());
        }
        //添加成功的时候返回一个空串
        return Result.success("");
    }
    //这里进行了两层包装，在统一返回的处理的地方还得判断类型是什么
    @RequestMapping("/getListByPage")
    public Result<PageResult<BookInfo>> getListByPage(PageRequest pageRequest) {
        log.info("查询列表信息, pageRequest:{}", pageRequest);
        //这之前的版本这里还得进行校验用户身份信息
        if(pageRequest.getCurrentPage()<=1) {
           return  Result.fail("参数不合法");
        }
        PageResult<BookInfo> pageResult = bookService.getListByPage(pageRequest);
        return Result.success(pageResult);
    }

    /**
     * 下面的目前没有问题了
     * @param bookId
     * @return
     */
    @RequestMapping("/queryBookById")
    public BookInfo queryBookById(Integer bookId){
        if(bookId<1) {
            log.error("非法图书ID,bookId:"+bookId);
            return null;
        }
        return bookService.queryBookById(bookId);
    }
    @RequestMapping("/updateBook")
    public Result updateBook(BookInfo bookInfo) {
        log.info("更新图书, updateBook:{}",bookInfo);
        if(bookInfo.getId()<1) {
            return Result.fail(false,"图书ID非法");
        }
        try{
            Integer count=bookService.updateBook(bookInfo);
            if(count<0) {
                return Result.fail(false,"更新失败请联系内部管理员");
            }
            return Result.success(true);
        } catch (Exception e) {
            log.error("更新图书失败, e:{}", e);
            return Result.fail(false,e.getMessage());
        }
    }
    @RequestMapping("/batchDelete")
    public Boolean batchDelete(@RequestParam("ids")List<Integer> ids) {
        if(ids==null) {
            //针对这些的返回结果在配置的时候，会统一进行包装
            //先打印日志
            log.error("参数不合法");
            return false;
        }
        try {
            Integer count=bookService.batchDelete(ids);
            if(count<0) {
                return false;
            }
            return true;
        } catch (Exception e) {
            log.error("批量删除数据失败，ids {}，e：{}",ids,e);
            return false;
        }
    }
}
